/**
 * 
 */
package runner;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import candy.BuilderChocoCandy;
import candy.BuilderCookies;
import candy.BuilderMilkCandy;
import candy.ChocoType;
import candy.Sweet;
import present.Constant;
import present.Present;
import property.UserProperty;

/**
 * @author
 * 
 */
public class Runner1 {

	/**
	 * @param args
	 * @throws IOException
	 * @throws Exception 
	 */
	public static void main(String[] args) throws IOException, Exception {

		String dirname = "src\\runner";
		File f1 = new File(dirname);

		String s1 = f1.getPath() + File.separator + "candy1.properties";
		String s2 = f1.getPath() + File.separator + "candy2.properties";
		String s3 = f1.getPath() + File.separator + "candy3.properties";

		
		Present present = new Present(Constant.maxPrice);

		UserProperty userProperty = new UserProperty();
		userProperty.propertyRead(s1);

		BuilderMilkCandy builderMilkCandy = new BuilderMilkCandy(
				Integer.parseInt(userProperty.getProperties().get("id")),
				userProperty.getProperties().get("name"),
				Double.parseDouble(userProperty.getProperties().get("albumens")),
				Double.parseDouble(userProperty.getProperties().get("fats")),
				Double.parseDouble(userProperty.getProperties().get("carbohydrates")),
				Double.parseDouble(userProperty.getProperties().get("weight")),
				Double.parseDouble(userProperty.getProperties().get("price")),
				Double.parseDouble(userProperty.getProperties().get("rateOfMilk")));
		if (present.addSweet(builderMilkCandy.getSweet())){
			System.out.println("Sweet is adding");
		}
		else {
			System.out.println("Sweet isn't adding");
		}
		
		userProperty.propertyRead(s2);
		BuilderChocoCandy builderChocoCandy = new BuilderChocoCandy(
				Integer.parseInt(userProperty.getProperties().get("id")),
				userProperty.getProperties().get("name"),
				Double.parseDouble(userProperty.getProperties().get("albumens")),
				Double.parseDouble(userProperty.getProperties().get("fats")),
				Double.parseDouble(userProperty.getProperties().get("carbohydrates")),
				Double.parseDouble(userProperty.getProperties().get("weight")),
				Double.parseDouble(userProperty.getProperties().get("price")),
				Double.parseDouble(userProperty.getProperties().get("rateOfMilk")),
				ChocoType.valueOf(userProperty.getProperties().get("chocoType")));

		if (present.addSweet(builderChocoCandy.getSweet())){
			System.out.println("Sweet is adding");
		}
		else {
			System.out.println("Sweet isn't adding");
		}	
		
			
				
		userProperty.propertyRead(s3);
		BuilderCookies builderCookies = new BuilderCookies(
				Integer.parseInt(userProperty.getProperties().get("id")),
				userProperty.getProperties().get("name"),
				Double.parseDouble(userProperty.getProperties().get("albumens")),
				Double.parseDouble(userProperty.getProperties().get("fats")),
				Double.parseDouble(userProperty.getProperties().get("carbohydrates")),
				Double.parseDouble(userProperty.getProperties().get("weight")),
				Double.parseDouble(userProperty.getProperties().get("price")),
				Double.parseDouble(userProperty.getProperties().get("rateOfFluor")));
			
		if (present.addSweet(builderCookies.getSweet())){
			System.out.println("Sweet is adding");
		}
		else {
			System.out.println("Sweet isn't adding");
		}
			
		System.out.println("Initial present");
		System.out.println(present.toString());
		
		
	
	
		

		present.sortByPrice();
		System.out.println("Sorted present");
		System.out.println(present.toString());

		for (Sweet sweet : present.getRangeCarbohydrates(Constant.minCarbohydrates,
				Constant.maxCarbohydrates)) {
			System.out.println(sweet.getName() + sweet.getCarbohydrates()
					+ "\n");
			}
		// Serialize observation 		
		FileOutputStream fileOutputStream = new FileOutputStream(f1.getPath()
				+ File.separator + "class.out");
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(
				fileOutputStream);
		objectOutputStream.writeObject(present);
		objectOutputStream.flush();
		objectOutputStream.close();
		System.out.println("Serialize version Total Weight"
				+ present.getTotalWeight());
		
		
		FileInputStream fileInputStream = new FileInputStream(f1.getPath()
				+ File.separator + "class.out");
		@SuppressWarnings("resource")
		ObjectInputStream objectInputStream1 = new ObjectInputStream(
				fileInputStream);
		Present present1 = (Present) objectInputStream1.readObject();
		System.out.println("Deserialize version Total Weight"
				+ present1.getTotalWeight());
		
		}

	}
